/**
 * 
 */
package cardbs.servlets;

import static cardbs.dao.DAOFactory.*;
import static cardbs.dao.WorkDAO.*;
import static cardbs.domain.Work.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cardbs.dao.DAOFactory;
import cardbs.dao.DepartmentDAO;
import cardbs.dao.IncludesDAO;
import cardbs.dao.JPAUtil;
import cardbs.dao.LocatedAtDAO;
import cardbs.dao.PartDAO;
import cardbs.dao.ServiceDAO;
import cardbs.dao.UseDAO;
import cardbs.dao.WorkDAO;
import cardbs.domain.Department;
import cardbs.domain.Deptype;
import cardbs.domain.Employee;
import cardbs.domain.Part;
import cardbs.domain.Service;
import cardbs.domain.Status;
import cardbs.domain.UserType;
import cardbs.domain.Work;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author marianna
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class DeletePartInfoservlet extends HttpServlet{
	public DeletePartInfoservlet() {
		// TODO Auto-generated constructor stub
	}
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param request
	 * @param response
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		// begin-user-code
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		Employee administrator = (Employee)session.getAttribute("Administrator");
		int partCode;
		int i=0;
		int sum;
		String select;
		sum = Integer.parseInt(request.getParameter("sum"));
		DAOFactory daoFactory = DAOFactory.getInstance();
		for(i=0; i<=sum; i++){
			select = request.getParameter("select"+i);
			if(select != null){
				partCode = Integer.parseInt(request.getParameter("part"+i));
				PartDAO partDAO = daoFactory.getPartDAO();
				UseDAO useDAO = daoFactory.getUseDAO();
				Boolean services = useDAO.findServicesByPartCode(partCode);
				if(services == true){
					try {
						HttpSession sess = request.getSession(true);
						sess.setAttribute("partInUse", "true");
						response.sendRedirect("/car-dbs-implementation/uipages/adminpages/diagrafiAllAntalaktikou.jsp");
						} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
				IncludesDAO includesDAO = daoFactory.getIncludesDAO();
				Boolean includes = includesDAO.findServicesByPartCode(partCode);
				if(includes == true){
					try {
					HttpSession sess = request.getSession(true);
					sess.setAttribute("partInUse", "true");
					response.sendRedirect("/car-dbs-implementation/uipages/adminpages/diagrafiAllAntalaktikou.jsp");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					} else {
					LocatedAtDAO locatedAtDAO = daoFactory.getLocatedDAO();
					Boolean located = locatedAtDAO.findPartsByPartCode(partCode);
					if(located == true){
						try{
						HttpSession sess = request.getSession(true);
						sess.setAttribute("partInUse", "true");
						response.sendRedirect("/car-dbs-implementation/uipages/adminpages/diagrafiAllAntalaktikou.jsp");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						} else {
						EntityManager em = JPAUtil.getCurrentEntityManager();
						EntityTransaction tx = em.getTransaction();
						tx.begin();
						Part part = partDAO.findPartByCode(partCode);
		        		tx.commit();
						partDAO.delete(part);
						try{
							HttpSession sess = request.getSession(true);
							sess.setAttribute("partDeleted", "true");
							response.sendRedirect("/car-dbs-implementation/uipages/firstPagePages/AdministratorViewLoginPage.jsp");
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						em.close();
					}	
				}
				}
   			//	} //try closed
			} // select is null
			
		} // for closed
}	
}